package com.qi.invokeclient.controller;

import com.qi.invokeclient.vo.InvokeRequest;
import com.qi.invokeclient.vo.InvokeResponse;
import io.dapr.client.DaprClient;
import io.dapr.client.domain.HttpExtension;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 姚炜琦
 * @Classname InvokeClientController
 * @Description TODO
 * @Date 2022/11/28 14:13
 */
@Slf4j
@RestController
public class InvokeClientController {
    @Autowired
    private DaprClient client;

    private static final String SERVICE_APP_ID = "invoke-server";
    private static final String METHOD_NAME = "send";

    @RequestMapping(value = "/say", method = RequestMethod.GET)
    public InvokeResponse say(String message) {
        log.info("send - message:{}", message);

        InvokeRequest request = new InvokeRequest();
        request.setMsgId(System.nanoTime());
        request.setMsgContent(message);

        InvokeResponse response = client.invokeMethod(
                SERVICE_APP_ID,
                METHOD_NAME,
                request,
                HttpExtension.POST,
                InvokeResponse.class).block();

        return response;
    }
}
